#ifndef _WEBSERVER_H_
#define _WEBSERVER_H_
#include <Arduino.h>
#include <Arduino_JSON.h>
#include "indexHtml.h"
#include "LocalStorage.h"
#include "Access.h"
#include "Controller.h"
#include "Setting.h"
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>

//#include <Ticker.h>
//Ticker ticker;
class WebServer {

private:
  AsyncWebServer server;                                                                                                          // 初始化服务器
  static AsyncWebSocket ws;                                                                                                              //初始化WebSocket
  const unsigned long interval;                                                                                                   // 清理间隔
  unsigned long previousMillis;                                                                                                   // 上一次清理的时间                                                                                                                  //创建AsyncWebSocket
  const char *PARAM_MESSAGE = "controlMessage";                                                                                   //响应头自定义数据头
  void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len);  //处理webSocket响应
  String dataContorl(const String &json);//json数据控制硬件并返回结果字符串
#ifdef ESP32
 static String wsData;
  static String wsDataNews;
#endif
public:
  WebServer();
  static void onEventReceived(const String& data);
  void begin();                                                           //开启服务
  void WebSocketCleaner();                                                //定时清理webSocket多余和断线的客户端(放在loop中)
  void handleClients();                                                   //直接清理webSocket多余和断线的客户端
  void notFound(AsyncWebServerRequest *request);                          //处理错误响应函数
  void responseData(AsyncWebServerRequest *request, const String &json);  //响应返回数据
  void responseDataWS(AsyncWebSocketClient *client, const String &json);  //响应返回数据webSocket
};
#endif  //_WEBSERVER_H_